package vip.xiaonuo.smzq.modular.psba.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaGtJbqk;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaGtYfjbqk;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaSqjl;
import vip.xiaonuo.smzq.modular.psba.entity.PsbaSqjlYfjbqk;
import vip.xiaonuo.smzq.modular.psba.entity.param.PsbaApprove;
import vip.xiaonuo.smzq.modular.psba.entity.param.PsbaYfxqParam;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaGtZbVo;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaYfxqVo;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbakczyclVo;
import vip.xiaonuo.smzq.modular.psba.entity.vo.Yfpfqk;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaGtYfjbqkMapper;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaSqjlMapper;
import vip.xiaonuo.smzq.modular.psba.mapper.PsbaSqjlYfjbqkMapper;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaGtYfjbqkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaGtZbService;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaGtZyclService;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaSqjlCzrzService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评审备案-压覆基本情况 服务实现类
 *
 * @author Kevin
 * @since 2024-05-15
 */
@Service
public class PsbaGtYfjbqkServiceImpl extends ServiceImpl<PsbaGtYfjbqkMapper, PsbaGtYfjbqk> implements IPsbaGtYfjbqkService {

    @Resource
    private PsbaGtYfjbqkMapper psbaGtYfjbqkMapper;
    @Resource
    private IPsbaGtZbService iPsbaGtZbService;//坐标
    @Resource
    private IPsbaGtZyclService iPsbaGtZyclService;//评审备案矿产资源储量
    @Resource
    private PsbaSqjlYfjbqkMapper psbaSqjlYfjbqkMapper;//申请压覆基本详情
    @Resource
    private IPsbaSqjlCzrzService iPsbaSqjlCzrzService;//更新审批状态
    @Resource
    private PsbaSqjlMapper psbaSqjlMapper;//填报审批记录


    @Override
    public Page<PsbaGtYfjbqk> page(long current, long size, String nd, String xzqhdm) {
        Page<PsbaGtYfjbqk> page = new Page<>(current, size);
        QueryWrapper<PsbaGtYfjbqk> queryWrapper = new QueryWrapper<>();
        if (xzqhdm!=null&&!xzqhdm.equals("")){
            xzqhdm = xzqhdm.replaceAll("0+$", "");
            queryWrapper.like("xzqdm",xzqhdm);
        }if (nd!=null&&!nd.equals("")){
            queryWrapper.like("psbarq",nd);
        }
        queryWrapper.orderByAsc("psbabh");
        page=psbaGtYfjbqkMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public PsbaGtYfjbqk getPsbaGtYfjbqk(String psbabh) {
        PsbaGtYfjbqk psbaGtYfjbqk = new PsbaGtYfjbqk();
        try{
            QueryWrapper<PsbaGtYfjbqk> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("psbabh",psbabh);
            psbaGtYfjbqk=psbaGtYfjbqkMapper.selectOne(queryWrapper);
        }catch (Exception e){

        }
        return psbaGtYfjbqk;
    }

    @Override
    public PsbaYfxqVo getPsbaYfxqVo(String psbabh) {
        PsbaYfxqVo psbaYfxqVo = new PsbaYfxqVo();

        //评审备案-压覆详情
        PsbaGtYfjbqk psbaGtYfjbqk = getPsbaGtYfjbqk(psbabh);
        if (psbaGtYfjbqk!=null){
            psbaYfxqVo.setPsbaGtYfjbqk(psbaGtYfjbqk);

            //压覆批复情况
            Yfpfqk yfpfqk = new Yfpfqk();
            BeanUtil.copyProperties(psbaGtYfjbqk, yfpfqk);
            psbaYfxqVo.setYfpfqk(yfpfqk);

        }

        //资源储量计算范围
        PsbaGtZbVo psbaGtZb = iPsbaGtZbService.getPsbaGtZbVo(psbabh);
        if (psbaGtZb!=null){
            psbaYfxqVo.setPsbaGtZb(psbaGtZb);
        }

        //评审备案矿产资源储量
        List<PsbakczyclVo> psbakczyclVoList=iPsbaGtZyclService.getPsbakczyclVoList(psbabh);
        if (psbakczyclVoList!=null&&psbakczyclVoList.size()>0){
            psbaYfxqVo.setPsbakczyclVoList(psbakczyclVoList);
        }



        return psbaYfxqVo;
    }

    @Override
    public void insertPsbaYfxqVo(PsbaYfxqParam psbaYfxqVo) {
        //评审压覆详情
        PsbaGtYfjbqk psbaGtYfjbqk=psbaYfxqVo.getPsbaGtYfjbqk();
        if (psbaGtYfjbqk!=null){
            //压覆批复情况
            Yfpfqk yfpfqk=psbaYfxqVo.getYfpfqk();
            if (yfpfqk!=null){
                BeanUtil.copyProperties(yfpfqk,psbaGtYfjbqk);
            }
            QueryWrapper<PsbaGtYfjbqk> queryWrapperMdb=new QueryWrapper<>();
            queryWrapperMdb.eq("psbabh",psbaGtYfjbqk.getPsbabh());
            psbaGtYfjbqkMapper.delete(queryWrapperMdb);
            //新增到原始数据表
            psbaGtYfjbqkMapper.insert(psbaGtYfjbqk);
            //删除申请压覆基本详情并同步最新数据
            QueryWrapper<PsbaSqjlYfjbqk> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("psbabh",psbaGtYfjbqk.getPsbabh());
            psbaSqjlYfjbqkMapper.delete(queryWrapper);
            PsbaSqjlYfjbqk psbaSqjlYfjbqk=  new PsbaSqjlYfjbqk();
            BeanUtil.copyProperties(psbaGtYfjbqk,psbaSqjlYfjbqk);
            psbaSqjlYfjbqk.setDjflbh(psbaYfxqVo.getPsbaGtYfjbqk().getDjflbh());
            psbaSqjlYfjbqk.setBglx(psbaYfxqVo.getPsbaGtYfjbqk().getBglx());
            psbaSqjlYfjbqk.setBgwj(psbaYfxqVo.getPsbaGtYfjbqk().getBgwj());
            psbaSqjlYfjbqkMapper.insert(psbaSqjlYfjbqk);
        }
         //坐标
             PsbaGtZbVo psbaGtZb=psbaYfxqVo.getPsbaGtZb();
              if (psbaGtZb!=null){
                  psbaGtZb.setPsbabh(psbaGtYfjbqk.getPsbabh());
                  iPsbaGtZbService.addPsbaGtZb(psbaGtZb);
              }
        PsbaApprove psbaApprove=new PsbaApprove();
        psbaApprove.setPsbabh(psbaGtYfjbqk.getPsbabh());
        psbaApprove.setType("7");
        iPsbaSqjlCzrzService.addRecord(psbaApprove);

        //更新审核状态已完成
        QueryWrapper<PsbaSqjl> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("psbabh",psbaGtYfjbqk.getPsbabh());
        PsbaSqjl psbaSqjl= psbaSqjlMapper.selectOne(queryWrapper);
        if (psbaSqjl!=null){
            psbaSqjl.setShzt("1");
            psbaSqjlMapper.updateById(psbaSqjl);
        }
    }
}
